Programming Questions for Practice
Write a program to perform transpose of a matrix.
#include <stdio.h>
#include <stdlib.h>
/* Write a program to perform transpose of a matrix. */
int main()
{
// Creating matrix of given size
int rows, columns;
printf("Enter the number of rows and columns: ");
scanf("%d%d", &rows, &columns);
int matrix_A[rows][columns], matrix_result[rows][columns];
// Reading the matrix
printf("Enter %d values for matrix A: ", rows*columns);
for(int row = 0; row < rows; row++)
for(int column = 0; column < columns; column++)
scanf("%d", &matrix_A[row][column]);
//display matrix
printf("\nMatrix A is\n");
for(int row = 0; row < rows; row++) {
for(int column = 0; column < columns; column++)
printf("%d\t", matrix_A[row][column]);
printf("\n");
}
// Logic for transpose of a matrix
for(int row = 0; row < rows; row++) {
for(int column = 0; column < columns; column++) {
matrix_result[column][row] = matrix_A[row][column];
}
}
// Display the result
printf("\nResult of transpose of A is\n");
for(int row = 0; row < rows; row++) {
for(int column = 0; column < columns; column++)
printf("%d\t", matrix_result[row][column]);
printf("\n");
}
return 0;
}
Code not available...
Code not available...